package com.dlb.andorid.activity;

import android.app.Dialog;
import android.content.Intent;
import android.os.Bundle;
import androidx.annotation.Nullable;
import androidx.recyclerview.widget.DefaultItemAnimator;
import androidx.recyclerview.widget.LinearLayoutManager;
import androidx.recyclerview.widget.RecyclerView;
import android.view.View;
import android.widget.EditText;
import android.widget.RadioButton;
import android.widget.TextView;

import com.chad.library.adapter.base.BaseQuickAdapter;
import com.chad.library.adapter.base.BaseViewHolder;
import com.dlb.andorid.App;
import com.dlb.andorid.R;
import com.dlb.andorid.XContract;
import com.dlb.andorid.XServerMsg;
import com.dlb.andorid.bean.AddressRetrieval;
import com.dlb.andorid.bean.Area;
import com.dlb.andorid.bean.City;
import com.dlb.andorid.bean.MyAddress;
import com.dlb.andorid.bean.Province;
import com.dlb.andorid.presenter.AddressPresenter;
import com.kwan.xframe.mvp.presenter.BasePresenter;
import com.kwan.xframe.mvp.view.activity.BaseCommonActivity;
import com.kwan.xframe.util.DialogFactory;

import java.util.List;

public class AddressAddActivity extends BaseCommonActivity implements XContract.IAddressView {

	AddressPresenter mPresenter;

	public static final int MODE_ADD = 1;
	public static final int MODE_EDIT = 2;
	private int mMode = MODE_ADD;
	private long mId;

	@Override
	protected BasePresenter getBasePresenter() {
		mPresenter = new AddressPresenter(this);
		mMode = (int) getIntentData("mode");

		if(mMode==MODE_EDIT){
			mId = (long) getIntentData("id");
		}

		return mPresenter;
	}

	TextView tv_province, tv_city, tv_area, tv_search;
	View ll_province, ll_city, ll_area, tv_save, ll_search;
	EditText et_name, et_phone, et_door, et_address;
	RadioButton rb_man;

	@Override
	protected void initViews() {
		super.initViews();

		ll_search = findViewById(R.id.ll_search);

		et_address = findViewById(R.id.et_address);

		rb_man = findViewById(R.id.rb_man);

		et_name = findViewById(R.id.et_name);
		et_phone = findViewById(R.id.et_phone);
		tv_search = findViewById(R.id.tv_search);
		et_door = findViewById(R.id.et_door);

		tv_save = findViewById(R.id.tv_save);
		tv_province = findViewById(R.id.tv_province);
		tv_city = findViewById(R.id.tv_city);
		tv_area = findViewById(R.id.tv_area);

		ll_province = findViewById(R.id.ll_province);
		ll_city = findViewById(R.id.ll_city);
		ll_area = findViewById(R.id.ll_area);

		et_phone.setText(App.mUserInfo.getPhone());

		ll_province.setOnClickListener(this);
		ll_city.setOnClickListener(this);
		ll_area.setOnClickListener(this);
		ll_search.setOnClickListener(this);
		tv_save.setOnClickListener(this);

	}

	@Override
	protected void initData() {
		showProgress("");
		mPresenter.getCity();
	}

	@Override
	protected int getTopViewId() {
		return 0;
	}

	@Override
	protected int getMainViewId() {
		return R.layout.activity_address_add;
	}

	@Override
	protected int getBottomViewId() {
		return 0;
	}

	@Override
	protected String getTitleTxt() {
		if (mMode == MODE_ADD)
			return "添加地址";
		else
			return "编辑地址";
	}

	@Override
	public String getPageName() {
		return null;
	}

	@Override
	public void onClick(View v) {
		super.onClick(v);

		if (v == ll_province) {
			showDialog(mProviceAdapter);
		} else if (v == ll_city) {
			showDialog(mCityAdapter);
		} else if (v == ll_area) {
			showDialog(mAreaAdapter);
		} else if (v == ll_search) {

			Bundle bundle = new Bundle();
			bundle.putLong("province_id", mProvince.getProvince_id());
			bundle.putLong("city_id", mCity.getCity_id());
			bundle.putLong("area_id", mArea.getArea_id());
			go2ActivityForResult(AddressSearchActivity.class, 100, bundle, 0, 0);


//			String key;
//			key = et_key.getText().toString();
//			if (key.isEmpty())
//				key = "";
//
//			showProgress("");
//			mPresenter.getAddressRetrieval(mProvince.getProvince_id(), mCity.getCity_id(), mArea.getArea_id(), key);

		} else if (v == tv_save) {

			if (mRetrieval != null) {

				String name = et_name.getText().toString();
				String phone = et_phone.getText().toString();
				String areaInfo = et_address.getText().toString();
				showProgress("");

				if(mMode==MODE_ADD)

				mPresenter.addAddress(name, rb_man.isChecked() ? 1 : 2, phone,
						mProvince.getProvince_id(),
						mCity.getCity_id(),
						mArea.getArea_id(),
						areaInfo, mRetrieval.getLatitude(), mRetrieval.getLongitude());
			else if(mMode==MODE_EDIT)
				mPresenter.editAddress(mId,name, rb_man.isChecked() ? 1 : 2, phone,
						mProvince.getProvince_id(),
						mCity.getCity_id(),
						mArea.getArea_id(),
						areaInfo, mRetrieval.getLatitude(), mRetrieval.getLongitude());

			} else {
				toastMsg("请先搜索地址");
			}
		}
	}


	Dialog dialog;

	private void showDialog(BaseQuickAdapter adapter) {
		View view = getLayoutInflater().inflate(R.layout.layout_dialog_list, null);
		dialog = DialogFactory.showCustomDialog3(this, view);
		RecyclerView recyclerView = view.findViewById(R.id.recycleView);

		recyclerView.setLayoutManager(new LinearLayoutManager(this));
		recyclerView.setItemAnimator(new DefaultItemAnimator());

		recyclerView.setHasFixedSize(true);
		recyclerView.setLayerType(View.LAYER_TYPE_SOFTWARE, null);
		recyclerView.setLayerType(View.LAYER_TYPE_HARDWARE, null);

		recyclerView.setAdapter(adapter);

		dialog.show();
	}

	@Override
	public void addAddressSuccess(String msg) {
		toastMsg(msg);
		finish();
	}

	Province mProvince;
	City mCity;
	Area mArea;
	AddressRetrieval mRetrieval;

	List<Province> mProvinces;
	List<City> mCitys;
	List<Area> mAreas;
//	List<AddressRetrieval> mAddressRetrievals = new ArrayList<>();

	BaseQuickAdapter<Province, BaseViewHolder> mProviceAdapter;
	BaseQuickAdapter<City, BaseViewHolder> mCityAdapter;
	BaseQuickAdapter<Area, BaseViewHolder> mAreaAdapter;
//	BaseQuickAdapter<AddressRetrieval, BaseViewHolder> mAddressRetrievalAdapter;

	void setCityText() {
		tv_province.setText(mProvince.getProvince_name());
		tv_city.setText(mCity.getCity_name());
		tv_area.setText(mArea.getArea_name());
		tv_search.setText("");
		mRetrieval = null;
	}

	@Override
	protected void onActivityResult(int requestCode, int resultCode, @Nullable Intent data) {
		super.onActivityResult(requestCode, resultCode, data);
		if (requestCode == 100) {
			mRetrieval = (AddressRetrieval) data.getSerializableExtra("data");
			tv_search.setText(mRetrieval.getName());
		}
	}

	@Override
	public void getAddressCitySuccess(List<Province> data) {
		mProvinces = data;
		mProvince = mProvinces.get(0);

		mCitys = mProvince.getCity();
		mCity = mCitys.get(0);

		mAreas = mCity.getArea();
		mArea = mAreas.get(0);

		setCityText();


		mProviceAdapter = new BaseQuickAdapter<Province, BaseViewHolder>(R.layout.list_item_string, mProvinces) {
			@Override
			protected void convert(BaseViewHolder helper, Province item) {
				helper.setText(R.id.tv_name, item.getProvince_name());
			}
		};

		mProviceAdapter.setOnItemClickListener(new BaseQuickAdapter.OnItemClickListener() {
			@Override
			public void onItemClick(BaseQuickAdapter adapter, View view, int position) {

				mProvince = mProvinces.get(position);
				mCity = mProvince.getCity().get(0);
				mArea = mCity.getArea().get(0);
				mCityAdapter.setNewData(mCitys);
				mAreaAdapter.setNewData(mAreas);
				setCityText();
				dialog.dismiss();
			}
		});

		mCityAdapter = new BaseQuickAdapter<City, BaseViewHolder>(R.layout.list_item_string, mCitys) {
			@Override
			protected void convert(BaseViewHolder helper, City item) {
				helper.setText(R.id.tv_name, item.getCity_name());
			}
		};

		mCityAdapter.setOnItemClickListener(new BaseQuickAdapter.OnItemClickListener() {
			@Override
			public void onItemClick(BaseQuickAdapter adapter, View view, int position) {
				mCity = mCitys.get(position);
				mAreas = mCity.getArea();
				mArea = mAreas.get(0);
				mAreaAdapter.setNewData(mAreas);
				setCityText();
				dialog.dismiss();
			}
		});

		mAreaAdapter = new BaseQuickAdapter<Area, BaseViewHolder>(R.layout.list_item_string, mAreas) {
			@Override
			protected void convert(BaseViewHolder helper, Area item) {
				helper.setText(R.id.tv_name, item.getArea_name());
			}
		};

		mAreaAdapter.setOnItemClickListener(new BaseQuickAdapter.OnItemClickListener() {
			@Override
			public void onItemClick(BaseQuickAdapter adapter, View view, int position) {

				mArea = mAreas.get(position);
				setCityText();
				dialog.dismiss();

			}
		});


//		mAddressRetrievalAdapter = new BaseQuickAdapter<AddressRetrieval, BaseViewHolder>(R.layout.list_item_string, mAddressRetrievals) {
//			@Override
//			protected void convert(BaseViewHolder helper, AddressRetrieval item) {
//				helper.setText(R.id.tv_name, item.getName());
//			}
//		};
//
//		mAddressRetrievalAdapter.setOnItemClickListener(new BaseQuickAdapter.OnItemClickListener() {
//			@Override
//			public void onItemClick(BaseQuickAdapter adapter, View view, int position) {
//				dialog.dismiss();
//				mRetrieval = mAddressRetrievals.get(position);
//				tv_address.setText(mRetrieval.getName());
//			}
//		});

	}

	@Override
	public void getAddressSuccess(List<MyAddress> data) {

	}


	@Override
	public void getAddressRetrievalSuccess(List<AddressRetrieval> data) {

//		dismissProgress();
//		mAddressRetrievals.clear();
//		mAddressRetrievals.addAll(data);
//		mAddressRetrievalAdapter.notifyDataSetChanged();
//		mRetrieval = data.get(0);
//		tv_address.setText(mRetrieval.getName());
//
//		showDialog(mAddressRetrievalAdapter);
	}

	@Override
	public void deleteAddSuccess(XServerMsg data) {

	}


}
